package com.folderit.apps.rooms.data;

import android.content.ContentUris;
import android.net.Uri;
import android.provider.BaseColumns;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by César on 13/12/2014.
 */
public class RoomsContract {
    // The "Content authority" is a name for the entire content provider, similar to the
    // relationship between a domain name and its website.  A convenient string to use for the
    // content authority is the package name for the app, which is guaranteed to be unique on the
    // device.
    public static final String CONTENT_AUTHORITY = "com.folderit.apps.rooms";

    // Use CONTENT_AUTHORITY to create the base of all URI's which apps will use to contact
    // the content provider.
    public static final Uri BASE_CONTENT_URI = Uri.parse("content://" + CONTENT_AUTHORITY);

    // Possible paths (appended to base content URI for possible URI's)
    // For instance, content://com.folderit.apps.rooms.data/room/ is a valid path for
    // looking at room data. content://com.folderit.apps.rooms.data/givemeroot/ will fail,
    // as the ContentProvider hasn't been given any information on what to do with "givemeroot".
    // At least, let's hope not.  Don't be that dev, reader.  Don't be that dev.
    public static final String PATH_ROOM = "room";
    public static final String PATH_RESERVATION = "reservation";
    public static final String PATH_USER = "user";

    // Format used for storing dates in the database.  Also used for converting those strings
    // back into date objects for comparison/processing.
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DATETIME_FORMAT = "yyyyMMddHHmmss";

    /**
     * Converts Date class to a string representation, used for easy comparison and database lookup.
     * @param date The input date
     * @return a DB-friendly representation of the date, using the format defined in DATE_FORMAT.
     */
    public static String getDbDateString(Date date){
        // Because the API returns a unix timestamp (measured in seconds),
        // it must be converted to milliseconds in order to be converted to valid date.
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        return sdf.format(date);
    }

    /**
     * Converts Date class to a string representation, used for easy comparison and database lookup.
     * @param date The input date
     * @return a DB-friendly representation of the date, using the format defined in DATE_FORMAT.
     */
    public static String getDbDateTimeString(Date date){
        // Because the API returns a unix timestamp (measured in seconds),
        // it must be converted to milliseconds in order to be converted to valid date.
        SimpleDateFormat sdf = new SimpleDateFormat(DATETIME_FORMAT);
        return sdf.format(date);
    }

    /**
     * Converts a dateText to a long Unix time representation
     * @param dateText the input date string
     * @return the Date object
     */
    public static Date getDateFromDb(String dateText) {
        SimpleDateFormat dbDateFormat = new SimpleDateFormat(DATE_FORMAT);
        try {
            return dbDateFormat.parse(dateText);
        } catch ( ParseException e ) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * Converts a datetimeText to a long Unix time representation
     * @param datetimeText the input date string
     * @return the Date object
     */
    public static Date getDateTimeFromDb(String datetimeText) {
        SimpleDateFormat dbDateFormat = new SimpleDateFormat(DATETIME_FORMAT);
        try {
            return dbDateFormat.parse(datetimeText);
        } catch ( ParseException e ) {
            e.printStackTrace();
            return null;
        }
    }

    /* Inner class that defines the table contents of the user table */
    public static final class UserEntry implements BaseColumns {

        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_USER).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_USER;
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_USER;

        // Table name
        public static final String TABLE_NAME = "user";

        // User name
        public static final String COLUMN_NAME = "name";

        // User phone number
        public static final String COLUMN_PHONE_NUMBER = "phone_number";

        // User email
        public static final String COLUMN_EMAIL = "email";

        public static Uri buildUserUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }
    }

    /* Inner class that defines the table contents of the reservation table */
    public static final class ReservationEntry implements BaseColumns {

        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_RESERVATION).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_RESERVATION;
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_RESERVATION;

        // Table name
        public static final String TABLE_NAME = "reservation";

        // Date and time the reservation begins
        public static final String COLUMN_BEGIN = "begin";

        // Date and time the reservation ends
        public static final String COLUMN_END = "end";

        // Event title or reservation short description
        public static final String COLUMN_EVENT_TITLE = "event_title";

        // Creation date
        public static final String COLUMN_CREATED = "created";

        // Reservation Id from the main app
        public static final String COLUMN_EXTERNAL_ID = "external_id";

        // Created by user id
        public static final String COLUMN_USER_KEY = "user_id";

        // Reserved room id
        public static final String COLUMN_ROOM_KEY = "room_id";

        public static Uri buildReservationUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }

        public static Uri buildReservationWithRoomAndDateRange(long roomId, String from, String to) {
            return CONTENT_URI.buildUpon()
                    .appendPath(String.valueOf(roomId))
                    .appendPath(from)
                    .appendPath(to)
                    .build();
        }

        public static String getRoomFromUri(Uri uri) {
            return uri.getPathSegments().get(1);
        }

        public static String getStartDateFromUri(Uri uri) {
            return uri.getPathSegments().get(2);
        }

        public static String getEndDateFromUri(Uri uri) {
            return uri.getPathSegments().get(3);
        }
    }

    /* Inner class that defines the table contents of the room table */
    public static final class RoomEntry implements BaseColumns {

        public static final Uri CONTENT_URI =
                BASE_CONTENT_URI.buildUpon().appendPath(PATH_ROOM).build();

        public static final String CONTENT_TYPE =
                "vnd.android.cursor.dir/" + CONTENT_AUTHORITY + "/" + PATH_ROOM;
        public static final String CONTENT_ITEM_TYPE =
                "vnd.android.cursor.item/" + CONTENT_AUTHORITY + "/" + PATH_ROOM;

        public static final String TABLE_NAME = "room";

        // Name of the room
        public static final String COLUMN_ROOM_NAME = "room_name";

        // Room short description
        public static final String COLUMN_DESCRIPTION = "description";

        // Room color id
        public static final String COLUMN_COLOR_ID = "color_id";

        // External id. Id of the room in the main app
        // Will be useful for maintaining the data synchronized with the main app.
        public static final String COLUMN_EXTERNAL_ID = "room_external_id";

        public static Uri buildRoomUri(long id) {
            return ContentUris.withAppendedId(CONTENT_URI, id);
        }
    }

    public static class RoomColor {
        public static final int RED = 0;
        public static final int BLUE = 1;
        public static final int GREEN = 2;
        public static final int YELLOW = 3;
        public static final int ORANGE = 4;
        public static final int VIOLET = 5;
    }
}
